home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / graphics.txt < prev    next >
Text File  |  1994-10-22  |  16KB  |  351 lines

  1. /*
  2. █████████████████████████████████████████████████████████████████████████████
  3. ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  4. ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ KNOW-HOW.GRAPHICS ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  5. ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  6. █████████████████████████████████████████████████████████████████████████████
  7.  
  8. This text contain complete description of KNOW_HOW.GRAPHICS 5.0x, the
  9. product in the 5th version of library. It provide the following features:
  10.     a) Using common code for any graphics library. It is realized now for
  11.        BGI and for Windows GDI. It is not difficult to add few lines of
  12.        code to use any other library, if it support basical operations
  13.        (lineto, moveto etc.)
  14.     b) Scrolling, Zooming, mirror reflection of image, rotations and even
  15.        complex rotations of image. Saying "complex rotations" I mean drawing
  16.        of the picture, rotated around x,y to alpha, then (result) around
  17.        x1,y1 to beta and so on. BGI fonts could be rotated too, filled BGI
  18.        fonts are also available (not all BGI font could be filled, of course).
  19. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  20. Additional (and very powerfull) features are available if KNOW-HOW.GRAPHICS
  21. is used together with KNOW-HOW.SLANG. SLANG is BASIC-like language, designed
  22. as CPP class. Its child classes could have additional operators, so
  23. KNOW-HOW.SLANG.GRAPHICS is the BASIC with access to drawing tools of current
  24. product. It provide to user the possibility to load graphical resourses
  25. (DOS or Windows, or other if he add some code) and interprete them in
  26. run-time. There are very many applications of this tool, like run-time
  27. changeable GUI, maketing (without stage of compilation) of BGI, GDI and so on
  28. applications, vector drawing tools (KNOW-HOW.VECTOR is the ready-to-use
  29. product of this type). Together with BASIC math. functions it could also be
  30. used for data plotting, spreadsheets and so on.
  31.  
  32. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  33. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ SERVICE FILES ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  34. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  35.  
  36. ▐▐▐▐▐▐▐ File SIMPLE.H contains some simplifications for often used names.
  37. Complete listing:
  38.  
  39. ▐ #ifndef __SIMPLE_H_
  40. ▐ #define __SIMPLE_H_
  41. ▐ typedef unsigned char uchar;
  42. ▐ typedef unsigned long ulong;
  43. ▐ typedef int bool;
  44. ▐ #define TRUE  1
  45. ▐ #define FALSE 0
  46. ▐ #define ON   TRUE
  47. ▐ #define OFF  FALSE
  48. ▐ #endif __SIMPLE_H_
  49. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  50. -----------------------------------------------------------------------------
  51. ▐▐▐▐▐▐▐ File GEOM.H contains geometry of KNOW-HOW. It defines geom. objects
  52. in C++ manner: loc(x, y) defines point, rect(x1, y1, x2, y2) - rectangle and
  53. so on.
  54. For example, instead of call to f(int x, int y) we could call f(loc l), where
  55. loc is defined in GEOM.H as
  56. ▐ struct loc
  57. ▐ { int X,Y;
  58. ▐ .....
  59. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  60. -----------------------------------------------------------------------------
  61. ▐▐▐▐▐▐▐ Files TRIGON.H and TRIGON.CPP contains structure with the overloaded
  62. sin(int) and cos(int) functions. This functions works more quick than standart
  63. versions but are not so flexible.
  64. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  65. -----------------------------------------------------------------------------
  66. ▐▐▐▐▐▐▐ Files KH_ERROR.H and KH_ERROR.CPP are the part of KNOW-HOW error
  67. handling system. The only variable is defined: kh_error_code. Functions of
  68. KNOW-HOW or user-defined functions could change it, default value is
  69. KH_SUCCESS:
  70.  
  71. ▐ enum { KH_SUCCESS, KH_UNKNOWN_ERROR, KH_FILE_ERROR, KH_BGI_ERROR,
  72. ▐       KH_MEMORY_ERROR, KH_USER_ERROR };
  73.  
  74. This list could be continued:
  75.  
  76. ▐ enum { KH_NEW_ERROR = KH_USER_ERROR + 1, ... }
  77. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  78. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ DRAWING MODULES ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  79. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  80.  
  81. ▐▐▐▐▐▐▐ File ABS_GRAF.H contains empty shablons of functions. We could
  82. derivate classes from it not thinking about concrete graphics library we
  83. shell use. It provide common code for any DOS, Windows and so on libraries,
  84. if them include the same functions. This set is only part of Paint class's
  85. functions set, described in PAINT.H:
  86.  
  87. ▐ struct AbstractGraphics
  88. ▐    {
  89. ▐    virtual void moveto(int x, int y) {}
  90. ▐    virtual void lineto(int x, int y) {}
  91. ▐    virtual void fillpoly(int numpoints, int* polypoints) {}
  92. ▐    };
  93. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  94. -----------------------------------------------------------------------------
  95. ▐▐▐▐▐▐▐ Files BGI_FONT.H and BGI_FONT.CPP contains class, which incapsulate
  96. BGI fonts (*.chr) description.
  97.     1. BGI fonts are not documented. I use existing format.
  98.     2. Abstract (non-BGI) graphics is used for moveto, lineto, drawpoly and
  99.        fillpoly operations. It should be overloaded in child classes for
  100.        concrete graf. libraries like BGI, Windows GDI and so on.
  101.     3. BGI header have some fields, which are not absolutely necessary (for
  102.        current class). I skip them and remarks are not complete for them.
  103.     4. No facilities like "vertical text" and so on is supported.
  104.        The reason is: I suppose to use drawing extender which have rotation
  105.        possibilities in Paint class.
  106. BGI font format is documented in the remarks to code in these files. So I
  107. give here only the brief explanation. BGI use two types of commands for
  108. drawing - moveto and lineto. To define non-filled fonts I use calls to the
  109. same (virtual) functions. To draw filled fonts I use fillpoly() function.
  110. In this case lineto calls are added to points array, and moveto call stop
  111. the polygon creation and begin next one. To use filled fonts you should
  112. use fonts which are drawn as group of closed polygons. Example - 'O' char:
  113.  
  114.        ┌───────┐
  115.        │▓▓▓▓▓▓▓│
  116.        │▓┌───┐▓│
  117.        │▓│   │▓│
  118.        │▓└─┬─┘▓│
  119.        │▓▓▓│▓▓▓│
  120.        └───┴───┘
  121. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  122. -----------------------------------------------------------------------------
  123. ▐▐▐▐▐▐▐ Files PAINT.H and PAINT.CPP. In the files described previously, we
  124. have incapsulated graphics primitives like lineto, moveto, drawchar and
  125. so on. Now the additional shell is included for rotation, mirror reflection,
  126. complex rotation, zooming and scrolling. All this operations are performed
  127. with abstract graphics calls, and not depend on library which you intend
  128. to use.
  129. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  130. ------------------------- WHAT IS COMPLEX ROTATION ? ------------------------
  131. ░Rotation could be simply processed if it is single operation. To use nested
  132. ░rotations we use stack of rotations info structures. Example of complex
  133. ░rotation:
  134. ░     void f1(int x, int y, int alpha) { ... perform rotation ... }
  135. ░     void f2(int x, int y, int alpha) { rotate(10,10,90); f1(x, y, alpha); }
  136. ░Function f2() call rotation procedure and then call f1(), which call another
  137. ░rotation. We could a) cancel first rotation and b) add first and second
  138. ░rotations to complex transformation. KNOW-HOW.GRAPHICS make possible both
  139. ░variances.
  140. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  141. The structure RInfo is used to keep information about single rotation, and
  142. stack of RInfo's is used (if R_STACK variable is set to 1) to keep complex
  143. rotation information.
  144. ▐ struct RInfo
  145. ▐    {
  146. ▐    loc center;
  147. ▐    int angle;
  148. ▐    };
  149. ///////////////
  150. ▐ struct Stack
  151. ▐    {
  152. ▐    int used, total;
  153. ▐    RInfo* list;
  154. ▐    Stack();
  155. ▐    ~Stack();
  156. ▐    void push(RInfo* r);
  157. ▐    void pop();
  158. ▐    void flash();
  159. ▐    };
  160. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  161. -----------------------------------------------------------------------------
  162. Class Paint give some additional facilities for any graphics library. Class
  163. does not contain calls to the concrete drawing functions - only to abstract
  164. prototypes.
  165. functions shold be overloaded in child classes for adaptation to concrete
  166. graphic interface (BGI, GDI, and so on).
  167.  
  168. The zoom and addzoom are deformation coeficients. You could use zoom in any
  169. part of program. Addzoom is used only once to set the additional deformation
  170. of the whole picture (for example if context is changed from screen to
  171. printer), for preview and so on.
  172.  
  173. ▐ class Paint : public BGI_Font
  174. ▐    {
  175. ▐    protected:
  176. ▐    loc zoom;                     // Image deformation
  177. ▐    loc add_zoom;                 // Additional deformation
  178. ▐    loc lt;                       // Scroll of part of picture
  179. ▐    loc add_scroll;               // Left - top clip of the whole picture
  180. ▐    int fill;                     // Fill flag
  181. ▐    loc center;                   // Rotation center
  182. ▐    int alpha;                    // Rotation angle
  183. ▐    bool R_STACK;                 // Use or not stack of rotations
  184. ▐    Stack* r_stack;               // Stack of rotations
  185. ▐    int mirror;
  186. ▐    public:
  187. ▐    Paint();
  188. ▐    ~Paint() { delete r_stack; }
  189. ▐    loc get_add_zoom() { return add_zoom; }
  190. ▐    loc get_zoom() { return zoom; }
  191. ▐    void set_mirror(int m) { mirror = m; } // X coord. if no rotation
  192. ▐    void set_stack(bool r) { R_STACK = r; r_stack->flash();
  193. ▐        rotate(loc(0, 0), 0); }
  194. ▐    void set_zoom(double x, double y) { zoom.X = x * add_zoom.X;
  195. ▐        zoom.Y = y * add_zoom.Y; }    // No out-of-range control !!!
  196. ▐    void set_add_zoom(double x, double y) { add_zoom.X = 100 * x;
  197. ▐        add_zoom.Y = 100 * y; }
  198. ▐    void set_scroll(int x, int y) { lt.X = x; lt.Y = y; }
  199. ▐    void set_add_scroll(int x, int y) { add_scroll.X = x;
  200. ▐        add_scroll.Y = y; }
  201. ▐    void rotate(loc c, int a);
  202. ▐    void set_fill(bool f) { fill = f; }
  203. ▐ // Using alpha and center returns rotated coordinates
  204. ▐    loc rot(int x, int y);
  205. ▐ // Return completely transformed point: rotated, zoomed and scrolled
  206. ▐    loc transform(int x, int y);
  207. ▐    };
  208. -----------------------------------------------------------------------------
  209. ▐▐▐▐▐▐▐ Files BGIPAINT.H and BGIPAINT.CPP. Classes in these files replace
  210. calls to empty graphics primitives functions to concrete calls of functions
  211. from BGI, GDI or from other drawing libraries. The #define directives set
  212. switches on one of libraries, user should unremark one of them to use it
  213. in program:
  214. ▐ #define DOS_BGI           // Realized
  215. ▐ // #define WIN_GDI        // Realized
  216. ▐ // #define FLASH_GRAPH    // Not Realized
  217. ▐ // #define OBJECT_GRAPH   // Not Realized
  218. ▐ // ........ to be continued
  219. The alternative solution is to use Options - Compiler - Code generation -
  220.    Defines in IDE to define the necessary variable
  221.  
  222. As the example, lets look to the code for DOS BGI.
  223.  
  224. ▐ #ifdef DOS_BGI           // If we unremark line with DOS_BGI (see above)
  225. ▐ #include <graphics.h>    // Include GBI header
  226. The following structure do nonthing but replace calls to f() {} to calls
  227. to f() { ::f(); }, where ::f() is BGI function.
  228. ▐ struct To_Paint
  229. ▐     {
  230. ▐     void putpixel(int x, int y) { ::putpixel(x, y, getcolor()); }
  231. ▐     void line(int xstart, int ystart, int xend, int yend)
  232. ▐     { line(xstart, ystart, xend, yend); }
  233. ▐     void moveto(int x, int y) { ::moveto(x, y); }
  234. ▐     void lineto(int x, int y) { ::lineto(x, y); }
  235. ▐     void fillpoly(int numpoints, int* polypoints)
  236. ▐     { ::fillpoly(numpoints, polypoints); }
  237. ▐     void circle(int x, int y, int radius) { ::circle(x, y, radius); }
  238. ▐     void ellipse(int x, int y, int stangle, int endangle, int xr, int yr)
  239. ▐     { ::ellipse(x, y, stangle, endangle, xr, yr); }
  240. ▐     void drawpoly(int numpoints, int far* points)
  241. ▐     { ::drawpoly(numpoints, points); }
  242. ▐     };
  243. ▐ #endif DOS_BGI
  244.  
  245. Then we combine Paint class possibilities to zoom, scroll, rotate (including
  246. complex rotations) and so on and To_Paint possibilities to output graphics
  247. primitives using different libraries.
  248.  
  249. ▐ class KH_Paint : public To_Paint, public Paint
  250. ▐     {
  251. ▐     public:
  252. ▐     KH_Paint() : Paint()   {}
  253. ▐     void putpixel(int x, int y);
  254. ▐     void line(int xstart, int ystart, int xend, int yend);
  255. ▐     void lineto(int x, int y);
  256. ▐     void moveto(int x, int y);
  257. ▐     void circle(int x, int y, int radius);
  258. ▐     void ellipse(int x, int y, int stangle, int endangle, int xr, int yr);
  259. ▐     void roundrect(int left, int top, int right, int bottom, int radius);
  260. ▐     void rectangle(int left, int top, int right, int bottom);
  261. ▐     void drawpoly(int numpoints, int far* points);
  262. ▐     void outtext(char far* str);
  263. ▐     };
  264.  
  265. «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«
  266. ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ATTENTION !!! ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  267.     1. The set of functions of KH_Paint class will be continued, it is
  268. possible that your version contains additional functions (setcolor() is the
  269. first candidate).
  270.     2. When the Windows compatible part (class To_Paint for Windows) was
  271. designed, I find some difficulties: Windows GDI is more powerfull than DOS
  272. BGI. It is also possible that when user will try to use his graphics library
  273. he find that it have some functions which are absent in both BGI and GDI.
  274. The solution is: use KH_Paint functions if possible, I think that it is much
  275. more than enought for most cases. Another way is to add new functions to
  276. KH_Paint (KH_Paint::draw_pig() {...} and so on, and modify To_Paint for DOS
  277. and Windows), If it is not possible - use direct calls to your lovely library
  278. - and forget about the compatibility with others.
  279.     3. KH_Paint above is restricted. The real listing of 5.0 version (and of
  280. course of 5.x) is Windows GDI - oriented. It means that if BGI does not
  281. contain some of GDI features, DOS BGI version of To_Paint class try to
  282. elulate them.
  283. ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  284. «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«
  285. -----------------------------------------------------------------------------
  286. Example.
  287. I add to *.CPP files (when possible) working examples of main() functions,
  288. to run it you need only to remove remarks. In this file I illustrate only
  289. the usage of KNOW-HOW.GRAPHICS with DOS BGI, but BGIPAINT file contains
  290. also example of Windows OWL usage.
  291.  
  292. ▐ void main()
  293. ▐     {
  294. ▐     int gdriver = DETECT, gmode;           // Standart BGI initialization
  295. ▐     initgraph(&gdriver, &gmode, "");       // see Borland help system
  296. ▐     KH_Paint* paint = new KH_Paint();      // Create new KH_Paint class
  297. ▐     paint->load("euro.chr");               // Load BGI font
  298. ▐     setcolor(LIGHTRED);
  299. ▐     setfillstyle(SOLID_FILL, LIGHTGRAY);
  300. ▐     paint->set_fill(ON);                   // This flag acts on drawpoly
  301. ▐                        // but not to fonts filling.
  302. ▐     paint->set_zoom(0.5, 0.8);             // Set picture deformation
  303. ▐     for(int i = 0; i < 360; i += 15)       // BGI fonts output with rotation
  304. ▐     {
  305. ▐     paint->rotate(loc(400, 200), i);
  306. ▐     paint->outtextxy(10, 200, "Hello, World !");
  307. ▐     }
  308. ▐     cleardevice();
  309. ▐     for(int i1 = 0; i1 < 360; i1 += 15)    // Primitives output with rotation
  310. ▐     {
  311. ▐     paint->rotate(loc(400, 200), i1);
  312. ▐     paint->roundrect(300, 100, 400, 200, 20);
  313. ▐     int poly[10] = {100, 100, 200, 100, 200, 200, 100, 200, 100, 100};
  314. ▐     paint->drawpoly(5, poly);
  315. ▐     }
  316. ▐     cleardevice();
  317. ▐     paint->set_stack(ON);                  // Switch complex rotations ON
  318. ▐     for(i = 0; i < 360; i += 30)           // First call rotate image to 30,
  319. ▐     {                                  // second to 60 and so on.
  320. ▐     paint->rotate(loc(400, 200), 30);
  321. ▐     paint->outtextxy(10, 200, "Hello, World !");
  322. ▐     }
  323. ▐     paint->set_stack(OFF);
  324. ▐     for(int j = 0; j < 360; j += 15)
  325. ▐     {
  326. ▐     paint->rotate(loc(400, 200), j);            // Ellipse rotations
  327. ▐     paint->ellipse(200, 200, 0, 360, 30, 20);
  328. ▐     paint->ellipse(300, 200, 50, 120, 30, 20);
  329. ▐     }
  330. ▐     delete paint;
  331. ▐     closegraph();
  332. ▐     }
  333.